package com.zm.oes.controller;

import com.zm.oes.entity.ApiResult;
import com.zm.oes.entity.JudgeQuestion;
import com.zm.oes.serviceimpl.JudgeQuestionServiceImpl;
import com.zm.oes.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 判断题控制层
 */
@RestController
public class JudgeQuestionController {

    @Autowired
    private JudgeQuestionServiceImpl judgeQuestionService;

    /**
     * 接受一个类型为JudgeQuestion的请求体（RequestBody）参数judgeQuestion，其中JudgeQuestion可能是一个Java Bean类，表示要
     * 添加的判断题信息
     */
    @PostMapping("/judgeQuestion")
    public ApiResult add(@RequestBody JudgeQuestion judgeQuestion) {
        //调用了一个名为judgeQuestionService的服务（或者称之为业务逻辑层）的方法add，并传入了judgeQuestion对象作为参数
        //该方法的目的是执行添加判断题信息的操作，并返回一个表示操作结果的整数值
        int res = judgeQuestionService.add(judgeQuestion);
        //根据res的值构建不同的API结果对象。如果添加操作成功（res不等于0），则返回HTTP状态码200和消息"添加成功"，否则返回状态码400和消
        // 息"添加失败"
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",res);
    }

    @GetMapping("/judgeQuestionId")
    public ApiResult findOnlyQuestionId() {
        JudgeQuestion res = judgeQuestionService.findOnlyQuestionId();
        return  ApiResultHandler.buildApiResult(200,"查询成功",res);
    }

    @PostMapping("/editJudgeQuestion")
    public ApiResult edit(@RequestBody JudgeQuestion judgeQuestion) {
        int res = judgeQuestionService.edit(judgeQuestion);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"修改成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"修改失败",res);
    }
}
